package org.cardoso.web.filter;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cardoso.application.ApplicationConstants;
import org.cardoso.persistence.EntityBean;

public class ApplicationFilter {

	protected String getApplicationContext(ServletRequest servletRequest) {
		return ((HttpServletRequest)servletRequest).getContextPath();
	}
	
	protected EntityBean getUserLogged(ServletRequest servletRequest) {
		return (EntityBean) ((HttpServletRequest)servletRequest).getSession().getAttribute(ApplicationConstants.USER_LOGGED);
	}
	
	protected boolean userLoggedIsNull(EntityBean userLogged) {
		return userLogged == null;
	}
	
	protected String getURL(ServletRequest servletRequest) {
		return ((HttpServletRequest)servletRequest).getRequestURL().toString();
	}
	
	protected String getURI(ServletRequest servletRequest) {
		return ((HttpServletRequest)servletRequest).getRequestURI();
	}
	
	protected void redirect(ServletResponse servletResponse, String redirect) {
		try {
			((HttpServletResponse)servletResponse).sendRedirect(redirect);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
